Imports System.Xml.XPath

<Serializable()> _
Public Class Database

#Region "Factory Methods"

    Private Sub New()

    End Sub


    Public Shared Function GetDatabase(ByVal dbName As String) As Database

        If String.IsNullOrEmpty(dbName) Then
            dbName = "entrez"

        End If

        Dim listDoc As XPathDocument

        Dim listNavigator As XPathNavigator


        listDoc = New XPathDocument(System.Web.HttpContext.Current.Server.MapPath(My.Settings.DatabaseConfigFile))

        listNavigator = listDoc.CreateNavigator

        Dim dbXPath As String

        dbXPath = String.Format(My.Settings.DatabaseXPath, dbName)

        Dim databaseNode As XPathNavigator

        Try

            databaseNode = listNavigator.SelectSingleNode(dbXPath)

            Dim db As New Database

            db.FriendlyName = databaseNode.GetAttribute("FriendlyName", String.Empty)

            db.Name = databaseNode.GetAttribute("Name", String.Empty)

            db.DefaultField = databaseNode.SelectSingleNode("Config").SelectSingleNode("DefaultField").GetAttribute("Name", String.Empty)

            db.FormatterFile = databaseNode.SelectSingleNode("Config").SelectSingleNode("FormatterFile").GetAttribute("Name", String.Empty)
            db.FormatterName = databaseNode.SelectSingleNode("Config").SelectSingleNode("Formatter").GetAttribute("Name", String.Empty)

            db.SummaryFile = databaseNode.SelectSingleNode("Config").SelectSingleNode("SummaryFile").GetAttribute("Name", String.Empty)
            db.SummaryName = databaseNode.SelectSingleNode("Config").SelectSingleNode("Summary").GetAttribute("Name", String.Empty)

            db.Fields = New List(Of ISearchFieldInfo)

            For Each field As XPathNavigator In databaseNode.SelectSingleNode("Fields").SelectChildren(XPathNodeType.Element)

                Dim fieldInfo As New SearchFieldInfo(field.GetAttribute("Name", String.Empty), _
                    field.GetAttribute("Tag", String.Empty), field.GetAttribute("Description", String.Empty))

                db.Fields.Add(fieldInfo)

            Next

            Return db

        Catch dirEx As IO.DirectoryNotFoundException


        Catch argEx As ArgumentException


        Catch invalidEx As InvalidOperationException


        Catch ex As Exception


        End Try

        Return Nothing

    End Function

#End Region


#Region "Fields"


    Private fieldsField As List(Of ISearchFieldInfo)
    Private formatterNameField As String
    Private formatterFileField As String
    Private friendlyNameField As String
    Private summaryNameField As String
    Private summaryFileField As String
    Private nameField As String
    Private defaultFieldNameField As String
#End Region

#Region "Properties"

    Public Property DefaultField() As String
        Get
            Return Me.defaultFieldNameField
        End Get
        Set(ByVal value As String)
            Me.defaultFieldNameField = value
        End Set
    End Property

    Public Property Fields() As List(Of ISearchFieldInfo)
        Get
            Return Me.fieldsField
        End Get
        Set(ByVal value As List(Of ISearchFieldInfo))
            Me.fieldsField = value
        End Set
    End Property

    Public Property FormatterFile() As String
        Get
            Return Me.formatterFileField
        End Get
        Set(ByVal value As String)
            Me.formatterFileField = value
        End Set
    End Property

    Public Property FormatterName() As String
        Get
            Return Me.formatterNameField
        End Get
        Set(ByVal value As String)
            Me.formatterNameField = value
        End Set
    End Property

    Public Property FriendlyName() As String
        Get
            Return Me.friendlyNameField
        End Get
        Set(ByVal value As String)
            Me.friendlyNameField = value
        End Set
    End Property

    Public Property SummaryFile() As String
        Get
            Return Me.summaryFileField
        End Get
        Set(ByVal value As String)
            Me.summaryFileField = value
        End Set
    End Property

    Public Property SummaryName() As String
        Get
            Return Me.summaryNameField
        End Get
        Set(ByVal value As String)
            Me.summaryNameField = value
        End Set
    End Property

    Public Property Name() As String
        Get
            Return Me.nameField
        End Get
        Set(ByVal value As String)
            Me.nameField = value
        End Set
    End Property

#End Region

#Region "Functions"


    Public Function CreateSummary(ByVal docSum As eUtils.DocSumType) As SummaryDataRecord

        Dim summary As SummaryDataRecord


        If String.IsNullOrEmpty(Me.SummaryFile) AndAlso Not String.IsNullOrEmpty(Me.SummaryName) Then

            summary = DirectCast(Activator.CreateInstance _
                (Type.GetType(Me.SummaryName)), SummaryDataRecord)

        ElseIf Not String.IsNullOrEmpty(Me.SummaryFile) AndAlso Not String.IsNullOrEmpty(Me.SummaryName) Then

            summary = DirectCast(Activator.CreateInstanceFrom _
                (Me.SummaryFile, Me.SummaryName).Unwrap, SummaryDataRecord)

        Else 'Field Not Found

            summary = New NullSummary

        End If


         summary.ImportDocSum(Me.Name, docSum)


        Return summary


    End Function

    Public Function CreateSearchFieldFormatter(ByVal fieldName As String) As ISearchFieldFormatter

        Dim formatter As ISearchFieldFormatter


        If String.IsNullOrEmpty(Me.FormatterFile) AndAlso Not String.IsNullOrEmpty(Me.FormatterName) Then

            formatter = DirectCast(Activator.CreateInstance _
                (Type.GetType(Me.formatterNameField)), ISearchFieldFormatter)

        ElseIf Not String.IsNullOrEmpty(Me.FormatterFile) AndAlso Not String.IsNullOrEmpty(Me.FormatterName) Then

            formatter = DirectCast(Activator.CreateInstanceFrom _
                (Me.FormatterFile, Me.formatterNameField), ISearchFieldFormatter)

        Else 'Field Not Found

            formatter = New NullSearchFieldFormatter

        End If


        If String.IsNullOrEmpty(fieldName) AndAlso String.IsNullOrEmpty(Me.DefaultField) Then

            formatter.SearchField = New SearchFieldInfo


            Return formatter

        End If


        If String.IsNullOrEmpty(fieldName) AndAlso Not String.IsNullOrEmpty(Me.DefaultField) Then


            fieldName = Me.DefaultField

        End If

        

        For Each item As ISearchFieldInfo In Me.Fields
            If item.Name = fieldName Then

                formatter.SearchField = item

                Exit For

            End If
        Next


        Return formatter

    End Function


#End Region


End Class
